package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.dto.CompleteInfo;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.LmsSaveUnVideoTask;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.LmsSaveVideoTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

public class LmsSaveVideo {

    public static void main(String[] args) throws Exception {



        int sync = 8;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();


        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李俊杰");
        teachers.add("阿迪力");
        for(String teacher:teachers){

            InputStream inputStream2 = new FileInputStream("libs//2022秋//teacher//"+teacher+".user");
            List<String> userString = IOUtils.readLines(inputStream2, "utf-8");
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[0];
                String pwd = strs[1];
                LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
                lmsWatchDTO.setUsername(userCode);
                lmsWatchDTO.setPassword(pwd);


                Map<Long, String> courseMap = Maps.newHashMap();
                Map<Long, String> allCourseMap =LmsUtil.getUserCourseMap(userCode,teacher);
                if(null == allCourseMap){
                    continue;
                }
                List<String> courses = Lists.newArrayList(
                        "习近平新时代中国特色社会主义思想",
                        "思想道德修养与法律基础",
                     //   "形势与政策",
                        "思想道德与法治",
                        "毛泽东思想和中国特色社会主义理论体系概论",
                        "中国近现代史纲要",
                        "公务员制度讲座",
                        "社区治理",
                        "马克思主义基本原理概论");

                for(Long kk:allCourseMap.keySet()){
                    String c = allCourseMap.get(kk);
                    if(courses.contains(c.trim())){
                        courseMap.put(kk,c);
                    }
                }


                Map<Long, String> newCourseMap = Maps.newHashMap();

                for(long id:courseMap.keySet()){
                    String subjectName = courseMap.get(id);


                    // D:\project\answer-tool\libs\lmsAnswerInfo\阿迪力\思想道德与法治
                    String completeInfoFilePath = "libs//lmsAnswerInfo//" +teacher+"//"+ subjectName + "//";
                    completeInfoFilePath = completeInfoFilePath + userCode + "-study.txt";

                    File completeInfoFile = new File(completeInfoFilePath);

                    if(subjectName.equals("形势与政策")){
                        System.err.println("??????????????????");
                        File f1 = new File( "libs//lmsAnswerInfo//" +teacher+"//"+ subjectName + "//"+ userCode + "-practice.txt");
                        System.err.println("f1.exists():"+f1.exists());
                        if(f1.exists()&&
                                JSONObject.parseArray(IOUtils.toString(new FileInputStream(f1), "UTF-8"),
                                        CompleteInfo.class).size()>=1){
                            continue;
                        }
                    }

                    if(completeInfoFile.exists()){


                        InputStream completeInfoStream = new FileInputStream(completeInfoFile);
                        String completeInfoStr = IOUtils.toString(completeInfoStream, "UTF-8");
                        List<CompleteInfo> completeInfoList = JSONObject.parseArray(completeInfoStr, CompleteInfo.class);
                        if(completeInfoList!=null){

                            if(subjectName.equals("毛泽东思想和中国特色社会主义理论体系概论")&&completeInfoList.size()>=8){
                                continue;
                            }
                            if(subjectName.equals("习近平新时代中国特色社会主义思想")&&completeInfoList.size()>=15){
                                continue;
                            }
                            if(subjectName.equals("中国近现代史纲要")&&completeInfoList.size()>=7){
                                continue;
                            }
                            if(subjectName.equals("马克思主义基本原理概论")&&completeInfoList.size()>=8){
                                continue;
                            } if(subjectName.equals("思想道德修养与法律基础")&&completeInfoList.size()>=7){
                                continue;
                            }
                            if(subjectName.equals("思想道德与法治")&&completeInfoList.size()>=7){
                                System.err.println("已经做完啦");
                                continue;
                            }
                        }

                    }

                    newCourseMap.put(id,subjectName);
                }


                if(newCourseMap.size()<1){
                    continue;
                }
                lmsWatchDTO.setCourseMap(newCourseMap);


                System.err.println("sss:"+ JSON.toJSONString(newCourseMap));
                watchQueue.add(lmsWatchDTO);
            }
        }

      //  Thread.sleep(2000000);

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new LmsSaveVideoTask("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }


    }



}
